package cn.mapway.ib.client.widget;

import cn.mapway.ib.client.ClientContext;
import cn.mapway.ib.client.rpc.AppInstallProxy;
import cn.mapway.ib.share.model.common.ApiResult;
import cn.mapway.ib.share.model.dir.DirFile;
import cn.mapway.ui.client.util.StringUtil;
import com.google.gwt.core.client.Callback;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import jsinterop.base.Js;

public class DirTree extends Tree {
    public DirTree() {
        load(null, "");
    }

    public void load(TreeItem parent, String path) {
        AppInstallProxy.listdir(path, new Callback<ApiResult<DirFile[]>, String>() {
            @Override
            public void onFailure(String reason) {
                ClientContext.alert(reason);
            }

            @Override
            public void onSuccess(ApiResult<DirFile[]> result) {
                if (result.isSuccess()) {
                    if (parent == null) {
                        clear();
                    }
                    for (int i = 0; i < result.data.length; i++) {
                        DirFile dirfile = result.data[i];
                        if (parent == null) {

                            TreeItem treeItem = addTextItem(extractName(dirfile.location));
                            treeItem.setUserObject(dirfile);
                        } else {
                            TreeItem treeItem = parent.addTextItem(extractName(dirfile.location));
                            treeItem.setUserObject(dirfile);
                            parent.setState(true);
                        }
                    }
                }
            }
        });
    }

    public String extractName(String location) {
        return StringUtil.extractName(location);
    }

    public void loadSubFolder(TreeItem treeItem, boolean fireEvent) {
        DirFile dirFile = Js.uncheckedCast(treeItem.getUserObject());
        if (dirFile.hasFetch) {
            if (fireEvent) {
                setSelectedItem(treeItem, true);
            }
            return;
        }
        dirFile.hasFetch = true;
        load(treeItem, dirFile.location);
        if (fireEvent) {
            setSelectedItem(treeItem, true);
        }
    }

    public void loadPath(String path) {
        TreeItem parent = findPathItem(null, path);
        if (parent != null) {
            loadSubFolder(parent, true);
        }
    }

    private TreeItem findPathItem(TreeItem parent, String path) {
        if (parent == null) {
            for (int i = 0; i < getItemCount(); i++) {
                TreeItem item = getItem(i);
                TreeItem pathItem = findPathItem(item, path);
                if (pathItem != null) {
                    return pathItem;
                }
            }
            return null;
        } else {
            DirFile dirFile = Js.uncheckedCast(parent.getUserObject());
            if (path.equals(dirFile.location)) {
                return parent;
            }
            if (!path.startsWith(dirFile.location)) {
                return null;
            }
            for (int j = 0; j < parent.getChildCount(); j++) {
                TreeItem child = parent.getChild(j);
                TreeItem subItem = findPathItem(child, path);
                if (subItem != null) {
                    return subItem;
                }
            }
            return null;
        }
    }
}
